package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.ServiceItem;
import com.bwie.service.ServiceItemService;
import com.bwie.mapper.ServiceItemMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author lenovo
 * @description 针对表【service_item(服务项表)】的数据库操作Service实现
 * @createDate 2025-07-11 14:32:40
 */
@Service
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper, ServiceItem>
        implements ServiceItemService {

    @Autowired
    ServiceItemMapper serviceItemMapper;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R getServerItemList(PageInfoVo pageInfoVo) {

        Page<ServiceItem> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());


        Page<ServiceItem> selectPage = serviceItemMapper.getServerItemList(page, pageInfoVo);

        return R.OK(selectPage);
    }

    @Override
    public R uploadImage(MultipartFile file) {

        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            if (storePath == null) {
                return R.ERROR(500, "上传失败");
            }
            String url = "http://192.168.10.131:8888/" + storePath.getFullPath();

            return R.OK(url);

        } catch (IOException e) {

            e.printStackTrace();
            return R.ERROR(500, "图片上传失败");

        }
    }

    @Override
    public R add(ServiceItem serviceItem) {

        serviceItemMapper.insert(serviceItem);

        return R.OK();
    }
}




